package com.scorcher.ilink;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

public class ListXmlParser {

	private static final String ns = null;

	public List parse(InputStream in) throws XmlPullParserException,
			IOException {
		try {
			XmlPullParser parser = Xml.newPullParser();
			parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
			parser.setInput(in, null);
			parser.nextTag();
			return readFeed(parser);
		} finally {
			in.close();
		}
	}

	private List<Entry> readFeed(XmlPullParser parser)
			throws XmlPullParserException, IOException {
		List<Entry> entries = new ArrayList<Entry>();

		parser.require(XmlPullParser.START_TAG, ns, "File");
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			String name = parser.getName();
			// Starts by looking for the Details tag
			if (name.equals("Details")) {
				entries.add(readEntry(parser));
			} else {
				skip(parser);
			}
		}
		return entries;
	}

	public static class Entry {
		public final String name;
		public final String size;
		public final String date;

		private Entry(String name, String date, String size) {
			this.name = name;
			this.date = date;
			this.size = size;
		}
		@Override
		public String toString(){
			return name+","+size+","+date;
		}
		
		public String getDate(){
			return date;
		}

	}
	
	// Parses the contents of an entry. If it encounters a name, date, or size
	// tag, hands them off
	// to their respective "read" methods for processing. Otherwise, skips the
	// tag.
	private Entry readEntry(XmlPullParser parser)
			throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, ns, "Details");
		String name = null;
		String date = null;
		String size = null;
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			String tag = parser.getName();
			if (tag.equals("Name")) {
				name = readName(parser);
			} else if (tag.equals("date")) {
				date = readDate(parser);
			} else if (tag.equals("size")) {
				size = readSize(parser);
			} else {
				skip(parser);
			}
		}
		return new Entry(name, date, size);
	}

	// Processes Name tags in the feed.
	private String readName(XmlPullParser parser) throws IOException,
			XmlPullParserException {
		parser.require(XmlPullParser.START_TAG, ns, "Name");
		String name = readText(parser);
		parser.require(XmlPullParser.END_TAG, ns, "Name");
		return name;
	}

	// Processes size tags in the feed.
	private String readSize(XmlPullParser parser) throws IOException,
			XmlPullParserException {
		parser.require(XmlPullParser.START_TAG, ns, "size");
		String size = readText(parser);
		parser.require(XmlPullParser.END_TAG, ns, "size");
		return size;
	}

	// Processes date tags in the feed.
	private String readDate(XmlPullParser parser) throws IOException,
			XmlPullParserException {
		parser.require(XmlPullParser.START_TAG, ns, "date");
		String date = readText(parser);
		parser.require(XmlPullParser.END_TAG, ns, "date");
		return date;
	}

	// For the tags Name,date and size, extracts their text values.
	private String readText(XmlPullParser parser) throws IOException,
			XmlPullParserException {
		String result = "";
		if (parser.next() == XmlPullParser.TEXT) {
			result = parser.getText();
			parser.nextTag();
		}
		return result;
	}

	private void skip(XmlPullParser parser) throws XmlPullParserException,
			IOException {
		if (parser.getEventType() != XmlPullParser.START_TAG) {
			throw new IllegalStateException();
		}
		int depth = 1;
		while (depth != 0) {
			switch (parser.next()) {
			case XmlPullParser.END_TAG:
				depth--;
				break;
			case XmlPullParser.START_TAG:
				depth++;
				break;
			}
		}
	}
}
